<?php
	$tplList = new XTemplate('view/product/list.htm');
	
	$q = _POST('q');
	
	$tplList->assign('baseUrl', $baseUrl);
	$_SESSION['category_name'] = 'Tất cả sản phẩm';
	$condition = ' 1=1 ';
	if($q){
		$condition .= 'AND product_name LIKE "%'.str_replace(' ', '%', $q).'%"';
	}
	$sql = "SELECT * FROM tblproduct WHERE $condition";
	$products 	= $db->fetchAll($sql);
	$limit 		= 12;
	$page		= (_POST('pi'))?_POST('pi'):1;
	$offset		= ($page-1)*$limit;
	$totalRow 	= count($products);
	if($totalRow<1){
		$tplList->parse('LIST.NOPRODUCT');
	}
	$sql = "SELECT * FROM tblproduct WHERE $condition LIMIT $offset,$limit";
	$products 	= $db->fetchAll($sql);
	foreach($products as $product){
		if($product['at_pronew'] == 1){
			$product['pronew'] = "<img class='icon-notify' src='$baseUrl/img/icon_new.png' />";
		}
		if($product['at_prosale'] == 1){
			$product['prosale'] = "<img class='icon-notify icon-sale' src='$baseUrl/img/sale-icon.gif' />";
		}
		if($product['price'])
			$product['price'] = formatPrice($product['price']) . ' đ';
		$image 		= $db->fetchOne("SELECT img_url FROM tblproduct_img WHERE product_id =".$product['product_id']."  AND basic_img =1");
	
		$product['imgurl'] = '/media/product/images/base/no-image.png';
		if($image['img_url']){
			$fileName = resizeImage( ROOT_APP.$image['img_url'], 162, 179, ROOT_APP.'/media/product/images/162x179/product-'.$product['product_id']);
			$product['imgurl'] = '/media/product/images/162x179/'.$fileName;
		}
		//List color
		$colors = $db->fetchAll("SELECT * FROM tblproduct_color WHERE product_id = ".$product['product_id'].' GROUP BY color_code');
		foreach($colors as $color){
			$tplList->insert_loop('LIST.LISTPRODUCT.COLOR', array('COLOR'=>$color));
		}	
		$tplList->insert_loop('LIST.LISTPRODUCT', array('LISTPRODUCT'=>$product));
	}
	$pages = pagenavigator($page, $totalRow, $limit, 5, "?c=product&a=list&q=$q");
	$tplList->assign('pages', $pages);
	$tplList->parse('LIST');
	$content = $tplList->text('LIST');